package com.dhp.aop;

import com.dhp.annotation.RateLimit;
import com.dhp.manager.RedisLimiterManager;
import com.dhp.model.entity.User;
import com.dhp.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
@Slf4j
public class RateLimiterInterceptor {

        @Resource
        private UserService userService;

        private final RedisLimiterManager redisLimiterManager;

        public RateLimiterInterceptor(RedisLimiterManager redisLimiterManager) {
            this.redisLimiterManager = redisLimiterManager;
        }

        @Around("@annotation(rateLimit)")
        public Object applyRateLimit(ProceedingJoinPoint joinPoint, RateLimit rateLimit) throws Throwable {
            RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();

            User loginUser = userService.getLoginUser(request);

            String key = rateLimit.key(); // 获取genChartByAi_的值
            long value = rateLimit.value();
            long duration = rateLimit.duration();

            redisLimiterManager.doRateLimit(key + "_" + loginUser.getId(), value, duration);

            // 执行被限流的方法
            return joinPoint.proceed();
        }

}
